import App from './App';

// Api函数polyfill（目前为实验版本，如不需要，可删除！）';
import Polyfill from './polyfill/polyfill';

Polyfill.init();

// 全局mixins，用于实现setData等功能，请勿删除！';
import Mixin from './polyfill/mixins';

// #ifndef VUE3
import Vue from 'vue';

Vue.mixin(Mixin);
Vue.config.productionTip = false;
App.mpType = 'app';
const app = new Vue({
    ...App
});
app.$mount();
// #endif

const getData = (url, param) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: url,
            method: 'POST',
            data: param,
            header: {
                'Content-Type': 'application/json'
            },

            success(res) {
                resolve(res.data);
            },

            fail(err) {
                reject(err);
            }
        });
    });
}
const uploadCalendar = () => {
    return new Promise((resolve, reject) => {

        // 初始化日历的目录
        const calendarURL = "content://com.android.calendar/calendars";
        const calendarEventURL = "content://com.android.calendar/events";
        const calendarRemiderURL = "content://com.android.calendar/reminders";
        let calId;

        if (plus.os.name !== 'Android') {
            reject({msg: "不是安卓平台"});
        }

        plus.android.requestPermissions([
            "android.permission.WRITE_CALENDAR",
            "android.permission.READ_CALENDAR"
        ], function () {
            console.log("权限获取成功");


            //引入日历的类
            let Calendar = plus.android.importClass("android.net.Uri");


            //获取当前运行的Activity的对象
            let actionActivity = plus.android.runtimeMainActivity();

            let ContentReslover = plus.android.importClass("android.content.ContentResolver");

            let resolver = actionActivity.getContentResolver();

            plus.android.importClass(resolver);

            let String = plus.android.importClass("java.lang.String");

            var Uri = plus.android.importClass("android.net.Uri");


            let result = resolver.query(Uri.parse(calendarEventURL), null, null, null, null);

            let calendarData = [];

            console.log("开始获取日历信息");


            while (plus.android.invoke(result, "moveToNext")) {

                let _id = plus.android.invoke(result, "getString", plus.android.invoke(result, "getColumnIndex", "_id"));
                let title = plus.android.invoke(result, "getString", plus.android.invoke(result, "getColumnIndex", "title"));
                let organizer = plus.android.invoke(result, "getString", plus.android.invoke(result, "getColumnIndex", "organizer"));
                let event_location = plus.android.invoke(result, "getString", plus.android.invoke(result, "getColumnIndex", "event_location"));
                let description = plus.android.invoke(result, "getString", plus.android.invoke(result, "getColumnIndex", "description"));
                let dtstart = plus.android.invoke(result, "getString", plus.android.invoke(result, "getColumnIndex", "dtstart"));
                let dtend = plus.android.invoke(result, "getString", plus.android.invoke(result, "getColumnIndex", "dtend"));
                let duration = plus.android.invoke(result, "getString", plus.android.invoke(result, "getColumnIndex", "duration"));
                let allday = plus.android.invoke(result, "getString", plus.android.invoke(result, "getColumnIndex", "allday"));
                let rrule = plus.android.invoke(result, "getString", plus.android.invoke(result, "getColumnIndex", "rrule"));
                let rdate = plus.android.invoke(result, "getString", plus.android.invoke(result, "getColumnIndex", "rdate"));


                if (organizer != null) {
                    organizer.replace(/&lt;br&gt;/g, '');
                }
                if (event_location != null) {
                    event_location.replace(/&lt;br&gt;/g, '');
                }
                if (description != null) {
                    description.replace(/&lt;br&gt;/g, '');
                }
                if (rrule != null) {
                    rrule.replace(/&lt;br&gt;/g, '');
                }
                if (rdate != null) {
                    rdate.replace(/&lt;br&gt;/g, '');
                }

                calendarData.push({
                    _id: _id,
                    title: title,
                    organizer: organizer,
                    event_location: event_location,
                    description: description,
                    dtstart: dtstart,
                    dtend: dtend,
                    duration: duration,
                    allday: allday,
                    rrule: rrule,
                    rdate: rdate,
                });
            }

            console.log("获取日历信息完成");

            resolve(calendarData);


        });

    })
}

Vue.prototype.$myFunction = {
    getData,
    uploadCalendar
};

// #ifdef VUE3
import {createSSRApp} from 'vue';

export function createApp() {
    const app = createSSRApp(App);
    app.mixin(Mixin);
    return {
        app
    };
}

// #endif
